using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace cb0tServer
{
    class Quotes
    {
        private static List<QuoteItem> items = new List<QuoteItem>();
        private static Random rnd = new Random();
        private static int last_used = 0;

        public static void Load()
        {
            items.Clear();

            if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/quotes.xml")) return;

            try
            {
                FileStream f = new FileStream(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/quotes.xml", FileMode.Open);
                XmlReader xml = XmlReader.Create(new StreamReader(f));

                xml.MoveToContent();
                xml.ReadSubtree().ReadToFollowing("quotes");

                while (xml.ReadToFollowing("quote"))
                {
                    QuoteItem a = new QuoteItem();

                    xml.ReadSubtree().ReadToFollowing("name");
                    a.name = Encoding.UTF8.GetString(Convert.FromBase64String(xml.ReadElementContentAsString()));
                    xml.ReadToFollowing("text");
                    a.text = Encoding.UTF8.GetString(Convert.FromBase64String(xml.ReadElementContentAsString()));
                    xml.ReadToFollowing("time");
                    a.time = uint.Parse(xml.ReadElementContentAsString());

                    items.Add(a);
                }

                xml.Close();
                f.Flush();
                f.Close();
            }
            catch { }
        }

        public static void Save()
        {
            try
            {
                XmlWriterSettings appearance = new XmlWriterSettings();
                appearance.Indent = true;
                XmlWriter xml = XmlWriter.Create(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/quotes.xml", appearance);

                xml.WriteStartDocument();
                xml.WriteStartElement("quotes");

                foreach (QuoteItem a in items)
                {
                    xml.WriteStartElement("quote");
                    xml.WriteElementString("name", Convert.ToBase64String(Encoding.UTF8.GetBytes(a.name)));
                    xml.WriteElementString("text", Convert.ToBase64String(Encoding.UTF8.GetBytes(a.text)));
                    xml.WriteElementString("time", a.time.ToString());
                    xml.WriteEndElement();
                }

                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                xml.Close();
            }
            catch { }
        }

        public static QuoteItem CallRandomQuote()
        {
            if (items.Count == 0) return null;
            
            int rq = (int)Math.Floor(rnd.NextDouble() * items.Count);

            if (items.Count == 1)
                return items[rq];

            while (rq == last_used)
                rq = (int)Math.Floor(rnd.NextDouble() * items.Count);

            last_used = rq;
            return items[rq];
        }

        public static QuoteItem[] Query(String q)
        {
            if (items.Count == 0) return null;

            List<QuoteItem> m = new List<QuoteItem>();

            foreach (QuoteItem i in items)
                if (i.text.ToUpper().IndexOf(q.ToUpper()) > -1)
                    m.Add(i);

            return m.Count == 0 ? null : m.ToArray();
        }

        public static QuoteItem[] CallAllQuotes()
        {
            if (items.Count == 0) return null;

            return items.ToArray();
        }

        public static void InsertQuote(UserObject userobj, String text)
        {
            items.Insert(0, new QuoteItem(userobj.name, text));
            Save();
        }
    }

    class QuoteItem
    {
        public String name;
        public String text;
        public uint time;

        public QuoteItem() { }

        public QuoteItem(String name, String text)
        {
            this.name = name;
            this.text = text;
            this.time = Helpers.UnixTime();
        }

        public String GetTime()
        {
            DateTime tmp = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            tmp = tmp.AddSeconds(this.time);
            return tmp.ToShortDateString() + " " + tmp.ToShortTimeString();
        }
    }
}
